import { GoodResponse } from './../../../util/response';
import { GoodsTypeAttributeService } from './../../../service/goods-type-attribute/goods-type-attribute.service';
import { GoodsImageService } from './../../../service/goods-image/goods-image.service';
import { GoodsAttrService } from './../../../service/goods-attr/goods-attr.service';
import { ResponseInterceptor } from './../../../interceptor/response/response.interceptor';
import config from 'src/config/config';
import { Body, Controller, Delete, Get, Patch, Post, UseInterceptors, Query, Response } from '@nestjs/common';
import { GoodsService } from 'src/service/goods/goods.service';
import * as mongoose from 'mongoose';

@Controller(`${config.adminPath}/goods`)
@UseInterceptors(ResponseInterceptor)
export class GoodsController {
    constructor(
        private readonly goodsService: GoodsService,
        private readonly goodsAttrService: GoodsAttrService,
        private readonly goodsImageService: GoodsImageService,
        private readonly goodsTypeAttributeService: GoodsTypeAttributeService,
    ) {}
    @Get()
    async find(@Query() query, @Response() res) {
        const { data, total } = await this.goodsService.find(query);
        res.send(
            GoodResponse({
                data,
                total,
            }),
        );
    }
    @Post()
    async create(@Body() body) {
        const { goods_image_list, attr_id_list, attr_value_list, goodsImageColorList = [] } = body;
        const goodsResult = await this.goodsService.create(body);
        if (goods_image_list && goods_image_list.length) {
            goods_image_list.forEach(async (img_url, index) => {
                await this.goodsImageService.create({
                    goods_id: goodsResult._id,
                    color_id: goodsImageColorList[index] ? new mongoose.Types.ObjectId(goodsImageColorList[index]) : '',
                    img_url,
                });
            });
        }
        attr_id_list.forEach(async (attribute_id, index) => {
            const { attr_type: attribute_type, title: attribute_title } = await this.goodsTypeAttributeService.find({ _id: attribute_id });
            await this.goodsAttrService.create({
                goods_id: goodsResult._id,
                goods_cate_id: goodsResult.cate_id,
                attribute_id,
                attribute_type,
                attribute_title,
                attribute_value: attr_value_list[index],
            });
        });
        return;
    }
    @Patch()
    async update(@Body() body) {
        const { _id, goods_image_list, attr_id_list, cate_id, attr_value_list, goodsImageColorList = [] } = body;
        const objectGoodsId = new mongoose.Types.ObjectId(_id);
        await this.goodsService.getModel().updateOne({ _id }, body);
        await this.goodsImageService.getModel().deleteMany({ goods_id: _id });
        if (goods_image_list && goods_image_list.length) {
            goods_image_list.forEach(async (img_url, index) => {
                await this.goodsImageService.create({
                    goods_id: objectGoodsId,
                    color_id: goodsImageColorList[index] ? new mongoose.Types.ObjectId(goodsImageColorList[index]) : '',
                    img_url,
                });
            });
        }
        attr_id_list.forEach(async (attribute_id, index) => {
            await this.goodsAttrService.getModel().deleteMany({ goods_id: _id });
            const { attr_type: attribute_type, title: attribute_title } = await this.goodsTypeAttributeService.find({ _id: attribute_id });
            await this.goodsAttrService.create({
                goods_id: new mongoose.Types.ObjectId(_id),
                goods_cate_id: new mongoose.Types.ObjectId(cate_id),
                attribute_id,
                attribute_type,
                attribute_title,
                attribute_value: attr_value_list[index],
            });
        });
        return;
    }
    @Delete()
    async delete(@Body() body) {
        const { _id: goods_id } = body;
        await this.goodsImageService.getModel().deleteMany({ goods_id });
        await this.goodsAttrService.getModel().deleteMany({ goods_id });
        return await this.goodsService.delete(body);
    }
}
